<?php
/**
 * 房源表
 */
namespace Common\Model;
use Think\Image\Driver\Gd;
use Think\Model;
use Think\Upload\Driver\Auth;

class MemberModel extends Model{
    //定义数据库连接信息
    protected $connection;
    //定义表名
    protected $tableName = 'member';

    /**
     * 更新用户头像到七牛云
     */
    public function upload($member_id,$url){
        $logo = $this->where("id=$member_id")->getField('logo');
        $pos = strpos($logo,'http://wx.qlogo.cn');
        if($pos!==false){
            $img_info = getimagesize($url);
            $Qiniu = new Auth('aBruJjiPVgqa4vVo1HwwH7dQ1mFei57U4Qfz2m7V','_L5LO2Emj9tkaVUfN59DVkdfd5zQTu4WXI0374xg');
            $key= uniqid();
            $extName=strrchr($key, '.');
            $pic_type = "";
            switch ($img_info['mime'])
            {
                case "image/jpeg":
                    $pic_type = ".jpg";break;
                case "image/png":
                    $pic_type = ".png";break;
                case "image/gif":
                    $pic_type = ".gif";break;
                default:
                    $pic_type = ".jpg";
            }
            $key = substr($key,0,strlen($key)-strlen($extName)).'_'.uniqid().$pic_type;
            //头像图片
            $im = imagecreate($img_info[0], $img_info[1]);
            imagedestroy($im);

            $imageInfos_1 = downloadimagefromweixin($url);
            $filenames_s = APP_PATH.'ewm/face/'.$key;
            $local_files = fopen($filenames_s, 'w');
            if (false !== $local_files) {
                if (false !== fwrite($local_files, $imageInfos_1["body"])) {
                    fclose($local_files);
                }
            }
            $product_pic_s = $filenames_s;
            $Image = new Gd();
            $Image->open($product_pic_s);
            $Image->thumb($img_info[0], $img_info[1]);
            $Image->save(APP_PATH.'ewm/face/'.$key);
            $imgUrl = $Qiniu->upload(APP_PATH.'ewm/face/'.$key,$key,'line-img');
            $this->where("id=$member_id")->save(['logo'=>$imgUrl]);
            return true;
        }
    }

}